package com.ljfiction.book.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.Baner.HomClassifyBean;
import com.ljfiction.book.Baner.HotbiaoqianBean;
import com.ljfiction.book.R;
import com.ljfiction.book.activitys.ClassfimuchActivity;
import com.ljfiction.book.http.HttpManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/7/5.
 */
public class ClassifyFragment extends BaseFragment {


    @BindView(R.id.biaoqianimage1)
    ImageView biaoqianimage1;
    @BindView(R.id.biaoqiantextview1)
    TextView biaoqiantextview1;
    @BindView(R.id.biaoqianimage2)
    ImageView biaoqianimage2;
    @BindView(R.id.biaoqiantextview2)
    TextView biaoqiantextview2;
    @BindView(R.id.biaoqianimage13)
    ImageView biaoqianimage13;
    @BindView(R.id.biaoqiantextview3)
    TextView biaoqiantextview3;
    @BindView(R.id.classifygridview)
    RecyclerView classifygridview;
    @BindView(R.id.refresh_class)
    SmartRefreshLayout refreshClass;
    Unbinder unbinder;
    private Context context;
    private List<HotbiaoqianBean.DataBean> classifydatas = new ArrayList<>();
    private List<HotbiaoqianBean.DataBean> allhotbiaoqian = new ArrayList<>();
    private BaseQuickAdapter homClassifyAdapter;
    private HotbiaoqianBean f;


    @Override
    @Nullable
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_classify, container, false);

        context = getActivity();
        intView();
        initData();
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    private void intView() {
        classifygridview.setAdapter(homClassifyAdapter = new BaseQuickAdapter<HotbiaoqianBean.DataBean, BaseViewHolder>(R.layout.hothuanyihuan_item, classifydatas) {
            @Override
            protected void convert(BaseViewHolder helper, HotbiaoqianBean.DataBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.biaoqiantext, item.getBookLableName());
                GradientDrawable gd = (GradientDrawable) helper.getView(R.id.biaoqiantext).getBackground();
                gd.setColor(Color.parseColor(item.getBookColor()));
            }
        });
        homClassifyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(context, ClassfimuchActivity.class);
                intent.putExtra("calssfy", classifydatas.get(position).getId());
                startActivity(intent);
            }
        });
        classifygridview.setLayoutManager(new GridLayoutManager(context, 4));
        refreshClass.setEnableLoadMore(false);
        refreshClass.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                classifydatas.clear();
                initData();
            }
        });
    }


    private void initData() {

        OkHttpUtils.get(HttpManager.IP + "/stacks-hotLableVersionTow")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        refreshClass.finishRefresh();
                        Gson g = new Gson();
                        try {
                            f = g.fromJson(s, HotbiaoqianBean.class);
                            allhotbiaoqian.addAll(f.getData());
                            classifydatas.addAll(3, allhotbiaoqian);
                            homClassifyAdapter.notifyDataSetChanged();
                            tochanggehot();
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void tochanggehot() {
        if (allhotbiaoqian.get(0).getBookLableName().length() > 3) {
            biaoqiantextview1.setMaxEms(2);
        }else {
            biaoqiantextview1.setMaxEms(3);
        }
        if (allhotbiaoqian.get(1).getBookLableName().length() > 3) {
            biaoqiantextview2.setMaxEms(2);
        }else {
            biaoqiantextview2.setMaxEms(3);
        }
        if (allhotbiaoqian.get(2).getBookLableName().length() > 3) {
            biaoqiantextview3.setMaxEms(2);
        }else {
            biaoqiantextview3.setMaxEms(3);
        }
        biaoqiantextview1.setText(allhotbiaoqian.get(0).getBookLableName());
        biaoqiantextview2.setText(allhotbiaoqian.get(1).getBookLableName());
        biaoqiantextview3.setText(allhotbiaoqian.get(2).getBookLableName());

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
